﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Linq.Expressions;
using Medianamik.Core.Interfaces;

namespace Medianamik.Core.Querying
{
    public class NodeWhere : IQueryWhere, IMemberInfo
    {
        private MemberExpression _expression;
        public NodeWhere(MemberExpression pExpression, Comparison pComparison, object pValue)
        {
            _expression = pExpression;
            Comparison = pComparison;
            Value = pValue;
        }

        private MemberInfo _memberInfo;
        public MemberInfo MemberInfo
        {
            get
            {
                if (_memberInfo == null)
                {
                    _memberInfo = _expression.Member;
                    if (!_memberInfo.DeclaringType.IsAssignableFrom(typeof(IQueryableNode)))
                    {
                        throw new ArgumentException("MemberExprssion must be from IQueryableNode");
                    }
                }
                return _memberInfo;
            }
        }

        public Comparison Comparison { get; private set; }

        public object Value { get; private set; }

        #region IQueryWhere Members

        object IQueryWhere.Subject
        {
            get { return MemberInfo; }
        }

        #endregion
    }
}
